class Restaurants::FoodTypesController < Restaurants::RestaurantsController
   before_action :get_food_types
   before_action :get_food_type,only: [:edit,:update,:destroy]

    def index
        @nav_title = "菜品类型管理"
        add_breadcrumb(create_nav '菜品类型管理','#')
        @food_types = FoodType.where(restaurant_id: session[:restaurant_id])
    end    

    def new
        set_new_breadcrumb
        @food_type = FoodType.new
    end    

    def edit
        set_edit_breadcrumb
    end    

    def update
        if @food_type.update_attributes(food_type_params)
           redirect_to action: :index
        else
            set_edit_breadcrumb
            render :edit
        end    
    end
    
    def create
        @food_type = FoodType.new(food_type_params)
        @food_type.restaurant_id = session[:restaurant_id]        
        if @food_type.save
           redirect_to action: :index
        else   
           set_new_breadcrumb 
           render :new
        end    
    end    

    def destroy
        @food_type.destroy
        redirect_to action: :index
    end    


    private
    def get_food_type
        @food_type = FoodType.find params[:id]
    end    

    def set_new_breadcrumb
        @nav_title = "新建菜品种类"
        add_breadcrumb create_nav('菜品种类管理','/restaurants/food_types')
        add_breadcrumb create_nav('新建菜品种类', '/restaurants/food_types/new')
    end   

    def set_edit_breadcrumb
        @nav_title = "修改#{@food_type.name}"
        add_breadcrumb(create_nav '菜品种类管理','/restaurants/food_types') 
        add_breadcrumb(create_nav @nav_title,"#")
    end

    def get_food_types
        @food_types = FoodType.where(restaurant_id: session[:restaurant_id])
    end  

    def food_type_params
        params.require(:food_type).permit(:name)
    end
end
